package com.project.zsxd.server.clocksleep.controller;


import com.alibaba.fastjson.JSONObject;
import com.project.zsxd.server.clocksleep.service.IClockSleepService;
import com.project.zsxd.server.clocksleep.vo.ClockSleepVO;
import com.project.zsxd.server.common.bean.Result;
import com.project.zsxd.server.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 睡眠打卡表 前端控制器
 * </p>
 *
 * @author kaler
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/clockSleep")
public class ClockSleepController {

    @Autowired
    private IClockSleepService clockSleepService;

    /**
     * 开始入睡
     * @return
     */
    @RequestMapping("startSleep")
    public Result startSleep() {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        clockSleepService.startSleep(currentUser);
        return Result.success(null);
    }

    /**
     * 补打卡
     * @return
     */
    @RequestMapping("fillSleep")
    public Result fillSleep(@RequestBody ClockSleepVO clockSleepVO) {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        clockSleepService.fillSleep(currentUser, clockSleepVO);
        return Result.success(null);
    }

    /**
     * 查询当天是否已入睡
     * 当天中午12点至次日中午12点入睡过都算当日已入睡
     * @return
     */
    @RequestMapping("queryIsSleepping")
    public Result queryIsSleepping() {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        boolean isSleep = clockSleepService.queryIsSleepping(currentUser);
        return Result.success(isSleep);
    }

    /**
     * 查询最近30天的入睡时间(旧)
     * @return 最近30天的入睡时间
     */
    @RequestMapping("queryLastMonthSleepTimes")
    public Result queryLastMonthSleepTimes() {
        JSONObject result = clockSleepService.queryLastMonthSleepTimesOld();
        return Result.success(result);
    }

    @RequestMapping("queryLastMonthSleepTimesNew")
    public Result queryLastMonthSleepTimesNew() {
        List<ClockSleepVO> result = clockSleepService.queryLastMonthSleepTimes();
        return Result.success(result);
    }


}
